package org.vergil.core.dao.dev;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.vergil.common.Tool;
import org.vergil.core.dto.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by vergil on 2/26/17.
 */
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    private Tool tool;

    private Logger logger = LoggerFactory.getLogger(UserRepositoryImpl.class);

    @Override
    public User save(User user) {
        logger.info("save user at userRepository !");
        String sql = "INSERT INTO `user`(`name`,`password`,`status`) VALUES (:name, :password, :status)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user);
        namedParameterJdbcTemplate.update(sql, parameterSource, keyHolder);
        user.setId(keyHolder.getKey().longValue());
        return user;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    private User UserMap(ResultSet resultSet)throws SQLException{
        User user = new User();
        user.setId(resultSet.getLong("id"));
        user.setName(resultSet.getString("name"));
        user.setPassword(resultSet.getString("password"));
        user.setStatus(resultSet.getInt("status"));
        return user;
    }

    @Override
    public User getUser(Map<String ,Object> param){
        StringBuilder sql = new StringBuilder("SELECT * FROM dev_db.user where 1=1 ");
        if (param.containsKey("id")){
            sql.append(" id = :id ");
        }
        if (param.containsKey("name")){
            sql.append("name = :name ");
        }
        return namedParameterJdbcTemplate.queryForObject(sql.toString(), param, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                return UserMap(resultSet);
            }
        });
    }

    @Override
    public List<User> getUserList(Map<String,Object> param){
        logger.info("get user list for UserRepositoryImpl");
        StringBuilder sql = new StringBuilder("SELECT * FROM dev_db.user where 1=1 ");
        if (param.containsKey("name")){
            sql.append("name like :name ");
        }
        return namedParameterJdbcTemplate.query(sql.toString(), param, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                return UserMap(resultSet);
            }
        });
    }
}
